package com.whmnx.chitchat.adapter.holder;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.tencent.imsdk.TIMCallBack;
import com.tencent.imsdk.TIMElem;
import com.tencent.imsdk.TIMFaceElem;
import com.tencent.imsdk.TIMFileElem;
import com.tencent.qcloud.tim.uikit.modules.chat.layout.message.holder.MessageContentHolder;
import com.tencent.qcloud.tim.uikit.modules.message.MessageInfo;
import com.tencent.qcloud.tim.uikit.utils.FileUtil;
import com.tencent.qcloud.tim.uikit.utils.ToastUtil;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.bean.ChatDetailsBean;
import com.whmnx.chitchat.main.MainActivity;
import com.whmnx.chitchat.ui.chat.ChatActivity;
import com.whmnx.chitchat.ui.my.MyFileActivity;
import com.whmnx.chitchat.utils.FileDownUtil;
import com.whmnx.chitchat.utils.UItils;
import com.whmnx.chitchat.view.CustomLoadingDialog;

/**
 * @author Administrator
 */
public class MessageFileHolder2 extends MessageContentHolder2 {

    private TextView fileNameText;
    private TextView fileSizeText;
    private TextView fileStatusText;
    private ImageView fileIconImage;
    private String[] split;

    public MessageFileHolder2(View itemView) {
        super(itemView);
    }


    @Override
    public int getVariableLayout() {
        return R.layout.message_adapter_content_file;
    }

    @Override
    public void initVariableViews() {
        fileNameText = rootView.findViewById(R.id.file_name_tv);
        fileSizeText = rootView.findViewById(R.id.file_size_tv);
        fileStatusText = rootView.findViewById(R.id.file_status_tv);
        fileIconImage = rootView.findViewById(R.id.file_icon_iv);
    }

    @Override
    public void layoutVariableViews(final ChatDetailsBean msg, final int position) {
       /* TIMElem elem = msg.getElement();
        if (!(elem instanceof TIMFileElem)) {
            return;
        }*/
        if (msg.isSelf()) {
            fileNameText.setTextColor(Color.WHITE);
            fileSizeText.setTextColor(Color.WHITE);
            fileIconImage.setImageResource(R.mipmap.file2);
        }else {
            fileNameText.setTextColor(Color.BLACK);
            fileSizeText.setTextColor(Color.BLACK);
            fileIconImage.setImageResource(R.mipmap.file4);
        }
      //  final TIMFileElem fileElem = (TIMFileElem) elem;
        Logger.i("file_msg",msg.getChatRecord_Content());
        if (msg.getChatRecord_Content().contains("##")){
            split = msg.getChatRecord_Content().split("##");
            final String path = split[0];
            Logger.i("file_msg",path);
            if (split.length >1) fileNameText.setText(split[1]);
            // fileNameText.setText(fileElem.getFileName());
            //   String size = FileUtil.FormetFileSize(split[1].getFileSize());
            if (split.length >2) fileSizeText.setText(split[2]);
        }else {
            fileNameText.setText(msg.getChatRecord_Content());
        }

        sendingProgress.setVisibility(View.GONE);
        fileStatusText.setVisibility(View.GONE);

        msgContentFrame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // ToastUtil.toastLongMessage("文件路径:" + path);
                UItils.showToastSafe("正在下载中...");
                FileDownUtil.downloadAPK(split[0],split[1]);
            }
        });
       /* if (msg.getStatus() == MessageInfo.MSG_STATUS_SEND_SUCCESS || msg.getStatus() == MessageInfo.MSG_STATUS_NORMAL) {
            fileStatusText.setText(R.string.sended);
        } else if (msg.getStatus() == MessageInfo.MSG_STATUS_DOWNLOADING) {
            fileStatusText.setText(R.string.downloading);
        } else if (msg.getStatus() == MessageInfo.MSG_STATUS_DOWNLOADED) {
            fileStatusText.setText(R.string.downloaded);
        } else if (msg.getStatus() == MessageInfo.MSG_STATUS_UN_DOWNLOAD) {
            fileStatusText.setText(R.string.un_download);
            msgContentFrame.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    msg.setStatus(MessageInfo.MSG_STATUS_DOWNLOADING);
                    sendingProgress.setVisibility(View.VISIBLE);
                    fileStatusText.setText(R.string.downloading);
                    fileElem.getToFile(path, new TIMCallBack() {
                        @Override
                        public void onError(int code, String desc) {
                            ToastUtil.toastLongMessage("getToFile fail:" + code + "=" + desc);
                            fileStatusText.setText(R.string.un_download);
                            sendingProgress.setVisibility(View.GONE);
                        }

                        @Override
                        public void onSuccess() {
                            msg.setDataPath(path);
                            fileStatusText.setText(R.string.downloaded);
                            msg.setStatus(MessageInfo.MSG_STATUS_DOWNLOADED);
                            sendingProgress.setVisibility(View.GONE);
                            msgContentFrame.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    ToastUtil.toastLongMessage("文件路径:" + path);

                                }
                            });
                        }
                    });
                }
            });
        }*/
    }
}
